﻿Imports System.Data.SqlClient
Public Class TimeWorkView
    Dim dt As New DataTable
    Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.DataGridView1)
        loadtable()
        'lbltotal.Text = dt.Rows.Count
        'caltime()
        'MessageBox.Show(dt.Rows(1).Item(3).ToString)
        'Dim dt2 = DataGridView1
        'MessageBox.Show(dt2.Rows(1).Cells(8).Value.ToString)
        caltime2()
        'Dim s1 = DateTime.Parse("11:00:00")
        'Dim s2 = DateTime.Parse("7:15:00")
        'Dim re3 = s1 - s2
        'MessageBox.Show(re3.Hours.ToString)
        'MessageBox.Show(re3.Minutes.ToString)
    End Sub

    Private Sub caltime2()
        Dim dt2 = DataGridView1
        Dim ho As Int16
        Dim mn As Int16
        Dim ho2 As Int16
        Dim mn2 As Int16
        For i = 0 To dt2.Rows.Count - 1
            If dt2.Rows(i).Cells(5).Value.ToString = "" Then
                dt2.Rows(i).Cells(5).Value = "7:00:00"
            End If
            If dt2.Rows(i).Cells(8).Value.ToString = "" Or dt2.Rows(i).Cells(5).Value.ToString > "17:00:00" Then
                dt2.Rows(i).Cells(8).Value = "17:00:00"
            End If
            If dt2.Rows(i).Cells(8).Value.ToString <= "13:00:00" Then
                dt2.Rows(i).Cells(8).Value = "13:00:00"
            End If
            If dt2.Rows(i).Cells(7).Value.ToString < "13:00:00" Then
                dt2.Rows(i).Cells(7).Value = "13:00:00"
            End If
            Dim st = DateTime.Parse(dt2.Rows(i).Cells(6).Value.ToString)
            Dim en = DateTime.Parse(dt2.Rows(i).Cells(5).Value.ToString)
            Dim st2 = DateTime.Parse(dt2.Rows(i).Cells(8).Value.ToString)
            Dim en2 = DateTime.Parse(dt2.Rows(i).Cells(7).Value.ToString)
            Dim re = st - en
            Dim re2 = st2 - en2
            ho += re.Hours
            mn += re.Minutes
            ho2 += re2.Hours
            mn2 += re2.Minutes
        Next
        Dim hos As Int16 = mn / 60
        If mn >= 60 Then
            lbltotal.Text = ho + hos & " ម៉ោង " & (mn Mod 60).ToString("00") & " នាទី" 'mn - (60 * hos)
        Else
            lbltotal.Text = ho & " ម៉ោង " & mn.ToString("00") & " នាទី"
        End If

        Dim hos2 As Int16 = mn2 / 60
        If mn2 >= 60 Then
            lbl2.Text = ho2 + hos2 & " ម៉ោង " & (mn2 Mod 60).ToString("00") & " នាទី" 'mn2 - (60 * hos2)
        Else
            lbl2.Text = ho2 & " ម៉ោង " & mn2.ToString("00") & " នាទី"
        End If

        Dim hos3 As Int16
        If mn + mn2 > 60 Then
            '(mn - (60 * hos) + mn2 - (60 * hos2)) >= 60 Then
            'hos3 = (mn - (60 * hos) + mn2 - (60 * hos2)) / 60
            hos3 = (mn + mn2) / 60
            'lblto.Text = ho + ho2 + hos + hos2 + hos3 & ":" & (mn - (60 * hos) + mn2 - (60 * hos2)) - (hos3 * 60)
            lblto.Text = ho + ho2 + hos3 & " ម៉ោង " & ((mn + mn2) Mod 60).ToString("00") & " នាទី"
        Else
            'lblto.Text = ho + ho2 + hos + hos2 & ":" & mn - (60 * hos) + mn2 - (60 * hos2)
            lblto.Text = ho + ho2 & " ម៉ោង " & (mn + mn2).ToString("00") & " នាទី"
        End If
        'MessageBox.Show(ho + ho2)
        'MessageBox.Show((mn + mn2) / 60)
        'MessageBox.Show(hos3 * 60)
    End Sub

    Private Sub caltime()
        Dim ho = 0
        Dim mn = 0
        Dim ho2 = 0
        Dim mn2 = 0
        For i = 0 To dt.Rows.Count - 1
            If dt.Rows(i).Item(3).ToString = "" Then
                dt.Rows(i).Item(3) = "7:00:00"
            End If
            If dt.Rows(i).Item(6).ToString = "" Or dt.Rows(i).Item(3).ToString > "17:00:00" Then
                dt.Rows(i).Item(6) = "17:00:00"
            End If
            If dt.Rows(i).Item(5).ToString < "13:00:00" Then
                dt.Rows(i).Item(5) = "13:00:00"
            End If
            Dim st = DateTime.Parse(dt.Rows(i).Item(4).ToString)
            Dim en = DateTime.Parse(dt.Rows(i).Item(3).ToString)
            Dim st2 = DateTime.Parse(dt.Rows(i).Item(6).ToString)
            Dim en2 = DateTime.Parse(dt.Rows(i).Item(5).ToString)
            Dim re = st - en
            Dim re2 = st2 - en2
            ho += re.Hours
            mn += re.Minutes
            ho2 += re2.Hours
            mn2 += re2.Minutes
        Next
        Dim hos As Int16 = mn / 60
        lbltotal.Text = ho + hos & ":" & mn - (60 * hos)
        Dim hos2 As Int16 = mn2 / 60
        lbl2.Text = ho2 + hos2 & ":" & mn2 - (60 * hos2)
        Dim hos3 As Int16 = ((mn - (60 * hos)) + (mn2 - (60 * hos2))) / 60
        lblto.Text = ho + ho2 + hos + hos2 + hos3 & ":" & (mn - (60 * hos)) + (mn2 - (60 * hos2)) - (hos3 * 60)
    End Sub
    Private Sub loadtable()
        cmd.Parameters("@S") = txtsearch.Text
        dt = cmd.ExecuteDataTable("SELECT wt.*,e.emp_name,e.emp_salary FROM worktime_tbl wt INNER JOIN employee_tbl e on e.emp_id=wt.emp_id WHERE UPPER(e.emp_name) LIKE UPPER('%'+@S+'%')")
        DataGridView1.DataSource = dt
    End Sub
    Private Sub loaddata()
        cmd.Parameters("@Se") = txtsearch.Text & "%"
        cmd.Parameters("@S") = dtpstart.Value
        cmd.Parameters("@E") = dtbend.Value
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT wt.*,e.emp_name,e.emp_salary FROM worktime_tbl wt INNER JOIN employee_tbl e on e.emp_id=wt.emp_id WHERE e.emp_name LIKE @Se and wt_date between @S and @E")
        'UPPER(e.emp_name) UPPER('%'+@Se+'%')
    End Sub

    Private Sub txtsearch_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        loaddata()
        caltime2()
    End Sub

    Private Sub lblto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblto.Click

    End Sub
End Class